<?php

namespace App\Http\Controllers;

use App\Http\Requests\ChapterRequest;
use App\Http\Requests\PushChapterRequest;
use App\Models\Chapter;
use App\Models\Repository;
use Illuminate\Http\Request;

class ChapterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @param Repository $repository
     * @param $repository_id
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function create(Request $request, Repository $repository)
    {
        $sequence = $repository->currentChapters()->max('sequence') + 1;

        return view('resources.chapter.create')->with(compact('repository', 'sequence'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param ChapterRequest $request
     *
     * @param Repository $repository
     * @param Chapter $chapter
     * @param $repository_id
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function store(ChapterRequest $request, Repository $repository, Chapter $chapter)
    {
        $chapter->fill($request->only(['title', 'content','type']));
        $chapter->save();
        $repository->attachChapter($chapter, $request->sequence);

        return redirect()->route('repositories.show', ['id' => $repository->id]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Chapter $chapter
     *
     * @return \Illuminate\Http\Response
     */
    public function show(Chapter $chapter)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Repository $repository
     * @param  \App\Models\Chapter $chapter
     *
     * @return \Illuminate\Http\Response
     */
    public function edit(Repository $repository, Chapter $chapter)
    {

        $sequence = $chapter->sequence();
        return view('resources.chapter.edit')->with(compact('chapter', 'sequence', 'repository','type'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\Models\Chapter $chapter
     *
     * @return \Illuminate\Http\Response
     */
    public function update(ChapterRequest $request, Chapter $chapter)
    {

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Chapter $chapter
     *
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy(Chapter $chapter)
    {
        $chapter->delete();

        return back();
    }

    public function push(PushChapterRequest $request, Repository $repository, Chapter $chapter)
    {
        $repository = $repository->load('sourceRepository');

        if ($repository->sourceRepository->chapters()->wherePivot('chapter_id', $chapter->id)->exists()) {
            return back()->withErrors('原作品已存在该版本章节');
        } elseif ($repository->sourceRepository->pulls()->wherePivot('chapter_id', $chapter->id)->exists()) {
            return back()->withErrors('该版本已推送到原作品，请等待原作品作者进行下一步操作');
        } else {
            $repository->sourceRepository->pulls()->attach($chapter, ['sequence' => $request->sequence]);
            return back();
        }
    }

    public function like(Chapter $chapter)
    {
        abort_if($chapter->isDislikedBy(), 409);
        $chapter->toggleLikeBy();
    }

    public function dislike(Chapter $chapter)
    {
        abort_if($chapter->isLikedBy(), 409);
        $chapter->toggleDislikeBy();
    }
}
